Path2D (SwiftUI Path)
Path2D is a vector path you build segment by segment, mirroring SwiftUI's Path. It is a
plain value: build it with line and curve commands, query its geometry, transform it, render it
with the <PathShape> view, or use it as a clipping / hit-testing / container shape.
The name
Path2Dis used becausePathis already taken by the file-path utilities. The drawing API itself follows SwiftUI'sPath.
Conventions
- Points are
{ x, y }, rectangles are{ x, y, width, height }. - Angles are in radians.
- Build methods are chainable (they return the path).
- Coordinates are absolute (in the shape's own coordinate space), exactly like SwiftUI's
Path.
Building a path
Build methods
clockwisefollows SwiftUI's convention. This is the opposite of the Web Canvascounterclockwiseflag — keep that in mind when porting Canvas code.
Rendering with <PathShape>
<PathShape> renders a Path2D as a SwiftUI shape. It accepts fill, stroke, trim and all
view modifiers, exactly like Rectangle or Circle. Provide either path (a prebuilt value)
or draw (a size-responsive builder, like <Canvas>).
Sizing is controlled by view modifiers (frame, padding, ...). With draw, the actual draw size
is the second argument; do not call setState inside the closure.
Geometry queries
These are computed on demand and return immediately.
Transforming
Transforms return a new Path2D (the original is unchanged).
As a clip / content / container shape
Pass a Path2D anywhere a shape is accepted:
To use a path as a mask, render it as a <PathShape> and pass that view to mask:
